﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfEfDAL;

namespace WpfEfDataEntry
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        
 
    private OMSEntities db = new OMSEntities();
    private CustomerCollection CustomerData;
    private ListCollectionView View;
    
    private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e) {
        var customerSource = ((CollectionViewSource)(this.FindResource("CustomerSource")));
        var query = from c in db.Customers 
                    where c.City == "Seattle" 
                    select c;


        this.CustomerData = new CustomerCollection(query, db);
        customerSource.Source = this.CustomerData;
        this.View = ((ListCollectionView)(customerSource.View));
    }
    
    private void btnFirst_Click(object sender, System.Windows.RoutedEventArgs e) {
        this.View.MoveCurrentToFirst();
    }
    
    private void btnPrevious_Click(object sender, System.Windows.RoutedEventArgs e) {
        if (this.View.CurrentPosition > 0) {
            this.View.MoveCurrentToPrevious();
        }
    }
    
    private void btnNext_Click(object sender, System.Windows.RoutedEventArgs e) {
        if (this.View.CurrentPosition < (this.View.Count - 1)) {
            this.View.MoveCurrentToNext();
        }
    }
    
    private void btnLast_Click(object sender, System.Windows.RoutedEventArgs e) {
        this.View.MoveCurrentToLast();
    }
    
    private void btnDelete_Click(object sender, System.Windows.RoutedEventArgs e) {
        if (this.View.CurrentPosition > -1) {
            this.View.RemoveAt(this.View.CurrentPosition);
        }
    }
    
    private void btnAdd_Click(object sender, System.Windows.RoutedEventArgs e) {
        var cust = (Customer)(this.View.AddNew());
        this.View.CommitNew();
    }
    
    private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e) {
        try {
            //check to see if there are any errors and if so, move the position to the first error
            var errors = from c in this.CustomerData
                         where c.Error != null
                         select c;

            if (errors.Count() > 0)
            {
                this.View.MoveCurrentTo(errors.First());
            }
            else
            {
                db.SaveChanges();
                MessageBox.Show("Customer data was saved.", this.Title, MessageBoxButton.OK, MessageBoxImage.Information);
            }

        }
        catch (Exception ex) {
            MessageBox.Show(ex.ToString());
        }
    }
}


       
}

